package com.itheima.serviceimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.OrderDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceimpl implements OrderService {

    @Autowired
    private OrderDao orderDao;



    @Override
    public Result addOrder(Map map) {

        //判断当天是否以设置预约
        String orderDate =(String) map.get("orderDate");
        try {
            Date date3 = DateUtils.parseString2Date(orderDate);
            OrderSetting orderSetting = orderDao.FindByDate(date3);
            int number = orderSetting.getNumber();//今日总可预约人数
            int reservations = orderSetting.getReservations();//以预约人数
// 1 ---->  判断当日是否以设置预约;
            if (orderSetting!=null){

//2   ---->  判断当天预约是否已满
                if(number<=reservations){ //判断可预约人数是否大于以预约人数
                   return new Result(false,MessageConstant.ORDER_FULL);//预约人数已满
                }

            }else {
                return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);//所选日期不能进行体检预约
            }


//3    ----->  判断用户是否为会员,如果不是则设置为会员
            Member member = orderDao.FindMemberByPhone(map.get("telephone").toString());
            if(member==null){
                //用户不是会员,将该用户添加为会员
                Date date = new Date(); // 当前日期
                String Date = DateUtils.parseDate2String(date);
                java.util.Date date1 = DateUtils.parseString2Date(Date);
                Member member1 = new Member(map.get("name").toString(),map.get("sex").toString(),map.get("idCard").toString(),map.get("telephone").toString(),date1);
                orderDao.addMember(member1);
//                非会员添加预约(用户ID,预约时间,预约类型,是否到诊,套餐ID)
                String orderDate1 =(String) map.get("orderDate"); //预约时间
                java.util.Date date2 = DateUtils.parseString2Date(orderDate1);
                //System.out.println("玛丽和"+date2);
                String setmealId_ = (String)map.get("setmealId");
                int setmealId = Integer.parseInt(setmealId_);//套餐ID
                Order order = new Order(member1.getId(),date2,Order.ORDERTYPE_WEIXIN,Order.ORDERSTATUS_NO,setmealId);
                //预约成功,把预约ID返回(在之前需要先更新预约人数
                orderDao.UpdateordersettingByDate(orderDate1);
                //添加预约
                orderDao.addOrder(order);
                return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());

            }else {
                //用户是会员,检查是否有重复预约
                //4   ---->  判断用户是否在同一日重复预约同一个套餐(用户ID,预约日期,套餐ID)
                Integer id = member.getId();//用户ID
                String orderDate1 = (String)map.get("orderDate"); //预约日期
                String setmealId_ = (String) map.get("setmealId"); //套餐ID
                int setmealId = Integer.parseInt(setmealId_);
                Order order = new Order(id,DateUtils.parseString2Date(orderDate1),setmealId);
                Order order1 = orderDao.FindByALL(order);
                if(order1!=null){
                    //用户重复预约
                    return new Result(false,MessageConstant.HAS_ORDERED);
                }
                    //是会员添加预约
                java.util.Date date2 = DateUtils.parseString2Date(orderDate1);
                Order order2 = new Order(id,date2,Order.ORDERTYPE_WEIXIN,Order.ORDERSTATUS_NO,setmealId);
                //预约成功,把预约ID返回(在之前需要先更新预约人数)
                orderDao.UpdateordersettingByDate(orderDate1);
                //添加预约
                orderDao.addOrder(order2);
                return new Result(true,MessageConstant.ORDER_SUCCESS,order2.getId());
            }

            //预约成功,更新当天预约人数



        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"预约失败,500");
        }


    }

    //根据预约ID查询此次预约的部分信息
    @Override
    public Map findById(Integer id) {
        Map orderMap = orderDao.findById(id);
        return orderMap;
    }
}
